package com.kunbo.cn.service.impl.core.customer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.customer.CustomerDataPermissionsDao;
import com.kunbo.cn.entity.core.customer.CustomerDataPermissions;
import com.kunbo.cn.service.core.customer.CustomerDataPermissionsService;

@Service
public class CustomerDataPermissionsImpl implements CustomerDataPermissionsService{

	@Autowired
	private CustomerDataPermissionsDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	
	
	

	@Override
	public Boolean delPermissions(String id) {
		// TODO Auto-generated method stub
		dao.delete(id);
		CustomerDataPermissions permissions = dao.findOne(id);
		if(permissions != null)
			return true;
		else
			return false;
	}

	@Override
	public List<Map<String, Object>> selPermissions(String userid) {
		// TODO Auto-generated method stub
		String sql = "select a.type,a.id,a.create_time,c.`name` as `create_name`, b.`name` as type_name  from business_customer_users_data_permissions a , business_customer_manage b,db_system_user_user c  where 1= 1 and a.customerid = b.id and a.create_pople = c.id and a.userid = '" + userid + "'";
		return jdbc.queryForList(sql);
	}

	@Override
	public CustomerDataPermissions savePermissions(String userid, Integer customerid, Integer create_pople,String type) {
		// TODO Auto-generated method stub
		 CustomerDataPermissions s = new  CustomerDataPermissions();
		 s.setUserid(userid);
		 s.setCustomerid(customerid);
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		 s.setCreate_time(format.format(new Date()));
		 s.setCreate_pople(create_pople);
		 s.setType(type);
		 s =  dao.saveAndFlush(s);
		 if(s != null)
			 return s;
		 else
			 return null;
	}

}
